<?php
include '../common.php';
$protocol = trim($_POST['protocol']);
if (empty($protocol)) {
	header("location:../register.php",302);
}

$rule = [
	'name'      => ['kong','用户名不能为空'],
	'password'  => ['kong','密码不能为空'],
	'password1' => ['kong','确定密码不能为空'],
	'email'     => ['kong','邮箱不能为空'],
	'email'     => ['is_email','邮箱不正确'],
];

$a = panduan($rule);
if($a['code'] == 0){
	exit($a['msg']);
}

$name      = trim($_POST['name']);
$password  = trim($_POST['password']);
$password1 = trim($_POST['password1']);
$email     = trim($_POST['email']);
$protocol  = trim($_POST['protocol']);


$strlen_password = (int)strlen($password);

if ($strlen_password < 6) {
	exit('密码太短,最少6位');
}elseif($strlen_password > 18) {
	exit('密码太长,最长18位');
}

if ($password !== $password1) {
	exit('两次密码不一致');
}
$password = md5($password);

if(aray($protocol)){
	exit('您没有同意用户协议');
}
$sql  = "SELECT * FROM public_user where name = '$name'";
$row = select($sql,$DB);
if ($row) {
	exit('用户名存在');
}
$time = time();
$sql  = "INSERT INTO public_user (name,password,email,hour) VALUES ('$name', '$password', '$email','$time')";
$row = insert($sql,$DB);
if (!$row) {
	exit('增加失败');
}
setcookie("name",$name,time()+3600,"/");
header("location:../../index.php",302);